import time
import emoji
from zhdate import ZhDate
import smtplib
from email.mime.text import MIMEText


def cal_days(stamp):
    '''
    

    Parameters
    ----------
    stamp : TYPE
        初始时间的时间戳.

    Returns
    -------
    None.

    '''
    days = int((time.time()-stamp)/86400)
    return days


def lunar_day_to_solar_day_stamp(lunar_day):
    '''将农历时间转为阳历时间并转为时间戳'''
    solar_day = ZhDate(int(lunar_day.split('-')[0]),
                       int(lunar_day.split('-')[1]),
                       int(lunar_day.split('-')[2])).to_datetime()
    return time.mktime(solar_day.timetuple())


def get_birthday_timestamp(birthday):
    tm_year = time.localtime(time.time()).tm_year   # 今天所在的年份
    birthday_timestamp_this_year = lunar_day_to_solar_day_stamp(birthday.format(tm_year))
    if birthday_timestamp_this_year-time.time() > 0:
        birthday_timestamp = birthday_timestamp_this_year
        is_today = False
    elif birthday_timestamp_this_year-time.time() < 0:
        birthday_timestamp = lunar_day_to_solar_day_stamp(birthday.format(tm_year+1))
        is_today = False
    else:
        birthday_timestamp = birthday_timestamp_this_year
        is_today = True
    return birthday_timestamp, is_today


def generate_all_msg(weather_msg, dictum_msg, sweet_words, birthday_msg):
    """
    today_msg_dic = {    'today_time': today_time,  # 今天的时间
                         'know_days': know_days,    # 相识多少天
                         'love_days': love_days,    # 在一起多少天
                         'city_name': city_name,    # 城市名字
                         'nowtemperature': nowtemperature,   # 当前温度
                         'low_c': low_c,    # 今天最低温度
                         'high_c': high_c,   # 今天最高温度
                         'nowshidu': nowshidu,   # 当前湿度
                         'nowpm25': nowpm25,    # 当前PM2.5
                         'notice': notice,    # 天气注意事项
                         'fx': fx,     # 风向
                         'fl': fl,     # 风力
                         'aqi': aqi,   # 空气指数
                         }
    """
    wife_birthday_msg = f"距离你生日还有{birthday_msg['wife_birthday_interval']}天" if not birthday_msg['is_today_wife'] else "生日快乐哦宝贝，爱你！"
    husband_birthday_msg = f"距离我生日还有{birthday_msg['husband_birthday_interval']}天" if not birthday_msg['is_today_husband'] else "祝小陈生日快乐！"
    today_mess = f"早上好呀:heart:小宝贝\n" \
                 f":date:今天是{weather_msg['today_time']}\n" \
                 f":couple:这是我们相识的第{weather_msg['know_days']}天\n" \
                 f":couple_with_heart:也是我们相爱的第{weather_msg['love_days']}天\n" \
                 f":girl:{wife_birthday_msg}\n" \
                 f":boy:{husband_birthday_msg}\n" \
                 f":city_sunrise:我们现在在{weather_msg['city_name']}\n" \
                 f":sunny:当前温度{weather_msg['nowtemperature']}°C\n" \
                 f":high_brightness:今天最低温度{weather_msg['low_c']}，最高温度{weather_msg['high_c']}\n" \
                 f":cyclone:当前湿度{weather_msg['nowshidu']}，PM2.5浓度{weather_msg['nowpm25']}\n" \
                 f":dash:今天刮{weather_msg['fx']}，风力{weather_msg['fl']}，空气指数为{weather_msg['aqi']}\n" \
                 f":sparkles:注意：{weather_msg['notice']}\n" \
                 f":revolving_hearts:今日情话：{dictum_msg}\n" \
                 f":kissing_heart:{sweet_words}"

    return emoji.emojize(today_mess, language='alias', variant='emoji_type')


def send_email(content):
    # 设置服务器所需信息
    # 163邮箱服务器地址
    mail_host = 'smtp.163.com'
    # 163用户名
    mail_user = 'chenwei213406'
    # 密码(部分邮箱为授权码)
    mail_pass = 'TOHPTSUBTXEYVSRI'
    # 邮件发送方邮箱地址
    sender = 'chenwei213406@163.com'
    # 邮件接受方邮箱地址，注意需要[]包裹，这意味着你可以写多个邮件地址群发
    receivers = ['1441758285@qq.com', '2817824490@qq.com']

    # 设置email信息
    # 邮件内容设置
    message = MIMEText(content, 'plain', 'utf-8')
    # 邮件主题
    message['Subject'] = '宝贝早上好呀'
    # 发送方信息
    message['From'] = sender
    # # 接受方信息
    # message['To'] = receivers[0]

    # 登录并发送邮件
    try:
        smtpObj = smtplib.SMTP()
        # 连接到服务器
        smtpObj.connect(mail_host, 25)
        # 登录到服务器
        smtpObj.login(mail_user, mail_pass)
        # 发送
        for receiver in receivers:
            # 接受方信息
            message['To'] = receiver

            smtpObj.sendmail(
                sender, receiver, message.as_string())
        # 退出
        smtpObj.quit()
        print('邮件发送成功！')
    except smtplib.SMTPException as e:
        print('error', e)  # 打印错误